package com.leezk.order.service.impl;

import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leezk.order.dto.OrderDto;
import com.leezk.order.entity.Menu;
import com.leezk.order.entity.Orders;
import com.leezk.order.entity.Student;
import com.leezk.order.mapper.MenuMapper;
import com.leezk.order.mapper.OrdersMapper;
import com.leezk.order.mapper.StudentMapper;
import com.leezk.order.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leezk.order.utils.JsonUtil;
import com.leezk.order.utils.OrderUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.*;


import static com.leezk.order.constant.CommonConstant.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author leotong
 * @since 2022-03-16
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    StudentLikeService service;

    @Override
    public Page<OrderDto> getOrders(Page<Orders> page) {
        Page<Orders> page1 = ordersMapper.selectPage(page, null);
        Page<OrderDto> result=new Page<>();
        result.setTotal(page1.getTotal());
        List<Orders> records = page1.getRecords();
        List<OrderDto> orderDtos=new ArrayList<>();
        for (Orders record : records) {
            OrderDto orderDto=new OrderDto();
            BeanUtils.copyProperties(record,orderDto);
            orderDto.setCreatTime(record.getCreatTime());
            List<OrderDto.OrderChild> orderChildren = JSON.parseArray(record.getDeail(), OrderDto.OrderChild.class);
            orderDto.setStudent(studentMapper.selectById(record.getSid()));
            orderDto.setOrderChildrens(orderChildren);
            orderDtos.add(orderDto);
        }
        result.setRecords(orderDtos);
        return result;
    }

    @Override
    public List<OrderDto> getAllOrders() {
        List<Orders> records = this.list();
        System.out.println(records);
        List<OrderDto> orderDtos=new ArrayList<>();
        for (Orders record : records) {
            OrderDto orderDto=new OrderDto();
            BeanUtils.copyProperties(record,orderDto);
            List<OrderDto.OrderChild> orderChildren = JSON.parseArray(record.getDeail(), OrderDto.OrderChild.class);
            orderDto.setStudent(studentMapper.selectById(record.getSid()));
            orderDto.setOrderChildrens(orderChildren);
            orderDtos.add(orderDto);
        }
        return orderDtos;
    }


    @Override
    public String createOrder(OrderDto orders) {
        Orders newOrder=new Orders();
        BeanUtils.copyProperties(orders,newOrder);
        String detail = JsonUtil.listToString(orders.getOrderChildrens());
        newOrder.setDeail(detail);
        newOrder.setUpdateTime(new Date());
        newOrder.setSid(Integer.valueOf(orders.getSid()));
        newOrder.setCreatTime(new Date());
        String orderId= OrderUtil.getInstance().GenerateOrder();
        newOrder.setId(orderId);
        newOrder.setIsPay(false);
        Student student = studentMapper.selectById(orders.getSid());
        newOrder.setPhone(student.getEmail());
        this.save(newOrder);
        service.initResult();
        return orderId;
    }

    @Override
    public boolean payOrder(String orderId) {
        System.out.println(orderId);
        Orders orders = getById(orderId);
        System.out.println(orders);
        List<OrderDto.OrderChild> orderChildren = JSON.parseArray(orders.getDeail(), OrderDto.OrderChild.class);
        for (OrderDto.OrderChild orderChild : orderChildren) {
            Menu menu = menuMapper.selectById(orderChild.getId());
            menu.setStock(Math.max(menu.getStock() - orderChild.getStock(), 0));
            menuMapper.updateById(menu);
        }
        orders.setIsPay(true);
        ordersMapper.updateById(orders);
        return true;
    }


}
